package com.yay.warehouse.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.yay.warehouse.Dashboard;
import com.yay.warehouse.model.LFType;
import com.yay.warehouse.model.Ticket;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class TicketsService extends DataProvider {
	private static Ticket currentTicket;
	private String serviceURL;
	private List<String> count;
	
	
	public static void setCurrentTicket(Ticket value) {
		currentTicket = value;
	}
	
	public static Ticket getCurrentTicket() {
		if(currentTicket == null) {
			currentTicket = new Ticket(LFType.LOST);
		}
		return currentTicket;
	}
	
	public String getServiceURL() {
		if(serviceURL == null) { 
			serviceURL = Dashboard.SERVER_URL + "TicketsDAO.php";
		}
		return serviceURL;
	}
	
	public List<Ticket> find(int type, int category, double latitude, double longitude) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", type);
		params.put("category", category);
		params.put("latitude", latitude);
		params.put("longitude", longitude);
		List<Ticket> ret = list(params);
		return ret;
	}
	
	public List<String> count() {
		if(count == null) {
			count = new ArrayList<String>();
			String data = countData();
			try {
		        JSONArray jArray = new JSONArray(data);
		        count.add(jArray.getJSONObject(0).getString("lost"));
		        count.add(jArray.getJSONObject(1).getString("found"));
			} catch(JSONException e) {
	        	Log.e(this.getClass().getName(), e.toString());
			}
		} 
		return count; 
	}
	
	public void save(Ticket ticket) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("date", ticket.getDate());
		params.put("type", ticket.getType());
		params.put("category", ticket.getCategory());
		params.put("name", ticket.getName());
		params.put("description", ticket.getDescription());
		params.put("latitude", ticket.getLatitude());
		params.put("longitude", ticket.getLongitude());
		params.put("contactPhone", ticket.getContactPhone());
		String id = saveData(params);
		try {
	        JSONArray jArray = new JSONArray(id);
	        if(jArray.length() > 0) {
	        	ticket.setId(jArray.getJSONObject(0).getInt("LAST_INSERT_ID()"));
	        }
		} catch(JSONException e) {
        	Log.e(this.getClass().getName(), e.toString());
		}
	}

	public void remove(Ticket ticket) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", ticket.getId());
		removeData(params);
	}
	
	public List<Ticket> list(Map<String, Object> params) {
		List<Ticket> ret = new ArrayList<Ticket>();
		String data = listData(params);
		if(data != null) {
			try {
		        JSONArray jArray = new JSONArray(data);
		        for(int i=0; i < jArray.length(); i++) {
		           ret.add(buildTicket(jArray.getJSONObject(i)));
		        }
			} catch(JSONException e) {
	        	Log.e(this.getClass().getName(), e.toString());
			}
		}
		return ret;
	}
	
	private Ticket buildTicket(JSONObject jsonObject) throws JSONException {
		Ticket ticket = new Ticket();
    	ticket.setId(jsonObject.getInt("id"));
    	ticket.setDate(jsonObject.getString("date"));
    	ticket.setType(jsonObject.getInt("type"));
    	ticket.setCategory(jsonObject.getInt("category"));
        ticket.setName(jsonObject.getString("name"));
        ticket.setDescription(jsonObject.getString("description"));
        ticket.setLatitude(jsonObject.getDouble("latitude"));
        ticket.setLongitude(jsonObject.getDouble("longitude"));
    	ticket.setContactPhone(jsonObject.getString("contact_phone"));
    	ticket.setActive(jsonObject.getInt("active"));
        return ticket;
	}

}
